package com.bdcom.comp;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.model.CanvasModel;
import com.bdcom.repository.CanvasModelRepository;
import com.bdcom.util.PlannerUtil;

/**
 * 项目导航栏
 * 
 * @author Administrator
 *
 */
@Component
public class InspectorPanel extends JScrollPane implements MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private CanvasModelRepository canvasModelRepository;
	@Autowired
	private CanvasPanel canvasPanel;

	private JTable inspectTable;

	private DefaultTableModel dm;

	private List<CanvasModel> modelList = new ArrayList<>();

	public InspectorPanel() {
		super();
		// TODO Auto-generated constructor stub
		String[] title = { PlannerUtil.getString("All Projects") };

		dm = new MyTableModel(new Object[][] {}, title);

		inspectTable = new JTable(dm);

		inspectTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

		this.setViewportView(inspectTable);

		inspectTable.addMouseListener(this);
	}

	@PostConstruct
	public void refreshInspector() {

		modelList = canvasModelRepository.findAll();

		removeModels();

		for (CanvasModel cvsModel : modelList) {

			dm.addRow(new Object[] { cvsModel.getProjectName() });
		}

	}

	private void removeModels() {
		// TODO Auto-generated method stub
		int i = dm.getRowCount() - 1;

		for (; i > -1; i--) {
			dm.removeRow(i);
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		//双击切换 提示保存文档
		if (e.getClickCount() == 2) {

			if (canvasPanel.getCanvasModel()!=null&&JOptionPane.showConfirmDialog(null, PlannerUtil.getString("save the change?"),
					PlannerUtil.getString("Yes or No"), JOptionPane.YES_OPTION) == JOptionPane.OK_OPTION) {
				canvasModelRepository.save(canvasPanel.getCanvasModel());
			}
			//切换项目
			int r = inspectTable.rowAtPoint(e.getPoint());

			canvasPanel.setCanvasModel(modelList.get(r));

		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void addModel(String projectName) throws Exception {
		// TODO Auto-generated method stub

		CanvasModel canvasModel = new CanvasModel();

		canvasModel.setProjectName(projectName);

		canvasModelRepository.save(canvasModel);

		refreshInspector();
	}

	public boolean containsModel(String projectName) {
		// TODO Auto-generated method stub
		if (canvasModelRepository.findOne(projectName) != null) {
			return true;
		}
		return false;
	}

	public void deleteModel() throws Exception {
		// TODO Auto-generated method stub
		// 获取所选项目名称和存储地址

		int x = inspectTable.getSelectedRow();

		if (x == -1) {
			JOptionPane.showMessageDialog(getParent(), PlannerUtil.getString("please choose one project!"));
			return;
		}
		canvasModelRepository.delete(modelList.get(x));
		refreshInspector();
	}

	public void saveModel(CanvasModel canvasModel) throws Exception {
		// TODO Auto-generated method stub
		if (canvasModel == null) {
			return;
		}
		canvasModelRepository.save(canvasModel);
		refreshInspector();
	}

	public CanvasModel getSelectedModel() {
		// TODO Auto-generated method stub

		int row = inspectTable.getSelectedRow();

		if (row == -1) {
			return null;
		} else {
			return modelList.get(row);
		}

	}
}

@SuppressWarnings("serial")
class MyTableModel extends DefaultTableModel {

	public MyTableModel(Object[][] data, Object[] columnNames) {
		super(data, columnNames);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		// TODO Auto-generated method stub
		return false;
	}

}
